package org.six11.ss.client;

import static org.six11.util.layout.FrontEnd.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.six11.ss.client.olive.OliveDrawingSurface;
import org.six11.ss.client.olive.OliveState;
import org.six11.ss.shared.GameStatusConstants;
import org.six11.ss.shared.Message;
import org.six11.util.adt.MultiState;
import org.six11.util.adt.SynchronizedQueue;
import org.six11.util.gui.InfoTextBox;
import org.six11.util.gui.PieChart;
import org.six11.util.layout.FrontEnd;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class PlayCard extends JPanel {

  private MultiState state;
  private InfoTextBox clueText;
  private PieChart pie;
  private JButton done;
  
  public PlayCard(MultiState ms) {
    super();
    this.state = ms;
    setBackground(Color.WHITE);
    OliveState oliveState = new OliveState(state);
    state.setValue(ClientStateNames.K_OLIVE_STATE, oliveState);
    OliveDrawingSurface canvas = new OliveDrawingSurface(oliveState);
    state.setValue(ClientStateNames.K_OLIVE_SURFACE, canvas);
    canvas.setPreferredSize(new Dimension(400, 400));
    
    // add listener to restart the timer when we enter play phase.
    state.addChangeListener(ClientStateNames.K_GAME_STATUS, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (GameStatusConstants.STATUS_PLAY.equals(evt.getNewValue())) {
          whackPieTimer();
        }
      }
    });
    
    state.addChangeListener(ClientStateNames.K_SKETCH_DONE, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        whackPieTimer();
      }
    });
    
    state.addChangeListener(ClientStateNames.K_ROLE, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        String roleType = evt.getNewValue() + "";
        if (roleType.equals("sketcher")) {
          clueText.setVisible(true);
          done.setVisible(true);
        } else if (roleType.equals("labeler")) {
          clueText.setVisible(false);
          done.setVisible(false);
        }
        repaint();
      }
    });
    
    state.addChangeListener(ClientStateNames.K_CLUE, new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        String clue = state.getString(ClientStateNames.K_CLUE);
        clueText.setText("I don't know what the clue was.");
        if (clue != null) {
          clueText.setText("");
          clueText.append(Color.BLACK, "The clue is: " + clue);
        }
      }
    });
    
//    state.addChangeListener(ClientStateNames.K_GAME_STATUS, new PropertyChangeListener() {
//      public void propertyChange(PropertyChangeEvent evt) {
//        String newVal = (String) evt.getNewValue();
//        if (!newVal.equals(GameStatusConstants.STATUS_REPLAY)) {
//          // in all cases EXCEPT replay, we want to clear the drawing surface.
//          OliveState os = (OliveState) state.getValue(ClientStateNames.K_OLIVE_STATE);
//          os.clearSurface();
//        }
//      }
//    });
    clueText = new InfoTextBox();
    clueText.setFont(new Font("Dialog", Font.PLAIN, 18));
    clueText.append(Color.BLUE, "No information yet.");
    pie = new PieChart();
    pie.setPreferredSize(new Dimension(100, 100));
    
    done = new JButton("Done");
    done.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        Message msg = new Message("sketchDone");
        SynchronizedQueue<Message> q = (SynchronizedQueue<Message>) state
            .getValue(ClientStateNames.K_TRANSMIT_QUEUE);
        q.add(msg);
      }
    });
    FrontEnd fe = new FrontEnd();

    fe.add(clueText, "clue");
    fe.add(canvas, "olive");
    fe.add(pie, "pie");
    fe.add(done, "done");
    fe.addRule(ROOT, N, "clue", N);
    fe.addRule(ROOT, W, "clue", W);
    fe.addRule("pie", W, "clue", E);

    fe.addRule(ROOT, N, "pie", N);
    fe.addRule(ROOT, E, "pie", E);

    fe.addRule("pie", S, "olive", N);
    fe.addRule(ROOT, W, "olive", W);
    fe.addRule(ROOT, E, "olive", E);
    
    fe.addRule(ROOT, S, "done", S);
    fe.addRule("olive", S, "done", N);
    fe.addRule(ROOT, W, "done", W);
    fe.addRule(ROOT, E, "done", E);

    setLayout(new BorderLayout());
    add(fe, BorderLayout.CENTER);
    
  }
  
  private void whackPieTimer() {
    long whenElapsed = (Long) state.getValue(ClientStateNames.K_ROUND_ELAPSED);
    pie.setTotalTime(whenElapsed - System.currentTimeMillis());
    pie.resetTimer();
    pie.startTimer();
  }

}
